package com.example.demo.demos.web;

import sun.net.www.http.HttpClient;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.URI;

import java.io.IOException;
import java.net.URL;


public class test_api {
    // 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
    public static void main(String[] args) {
        try {

            // 创建URL对象
            URL url = new URL("https://www.google.com");
            // 打开连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            // 设置请求方法
            connection.setRequestMethod("GET");

            // 发送请求并接收响应
            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) { // 成功响应
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String inputLine;
                StringBuilder response = new StringBuilder();

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();

                // 打印响应体
                System.out.println(response.toString());
            } else {
                System.out.println("GET请求未成功，响应码：" + responseCode);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
